package com.my.httpapi.api.baseView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import com.my.httpapi.api.baseApi.ComObserver;
import com.my.httpapi.api.baseApiInterface.OnLoadingListener;
import com.my.httpapi.api.baseApiInterface.ProgressManageError;

import java.lang.ref.WeakReference;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import io.reactivex.disposables.CompositeDisposable;
import static com.google.gson.internal.$Gson$Types.getRawType;

/**
 * Author:guohaijin
 * Date:
 * Description:
 */
public abstract class BasePresenter<T, M extends BaseModelInter> {
    private WeakReference<T> weakReference;
    private CompositeDisposable mCompositeDisposable;
    protected M model;

    public void attach(T t) {
        if (weakReference == null) {
            weakReference = new WeakReference<>(t);
        }
        if (model == null) {
            model = createModel();
        }
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
    }

    public void deAttach() {
        if (weakReference != null) {
            weakReference.clear();
            weakReference = null;
        }
        if (model != null) {
            model = null;
        }
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
            mCompositeDisposable = null;
        }
    }

    public boolean isViewAttached() {
        return weakReference != null && weakReference.get() != null;
    }

    private M createModel() {
        try {
            Type superClass = getClass().getGenericSuperclass();
            Type type = ((ParameterizedType) superClass).getActualTypeArguments()[1];
            Class<?> clazz = getRawType(type);
            return (M) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public T getView() {
        if (weakReference != null) {
            return weakReference.get();
        }
        return null;
    }

    // protected abstract M getModel();

    // protected abstract Map<String, List<String>> getMap();

    /**
     * @显示默认loading的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener) {
        return request(onLoadingListener, true);
    }

    /**
     * @自定义是否显示loading的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, boolean isLoading) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, (Activity) getView(), isLoading);
    }

//    protected ComObserver request(OnLoadingListener onLoadingListener, Context context, boolean isLoading) {
//        return new ComObserver(onLoadingListener, context, isLoading);
//    }

    /**
     * @自定义loading样式的请求
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, Context context, AlertDialog alertDialog) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, alertDialog, context, true);
    }

    /**
     * @显示默认loading的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, ProgressManageError progressManageError) {
        return request(onLoadingListener, progressManageError, true);
    }

    /**
     * @自定义是否显示loading的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, ProgressManageError progressManageError, boolean isLoading) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, progressManageError, (Activity) getView(), isLoading);
    }

//    protected ComObserver request(OnLoadingListener onLoadingListener, ProgressManageError progressManageError, Context context, boolean isLoading) {
//        return new ComObserver(onLoadingListener, progressManageError, context, isLoading);
//    }

    /**
     * @自定义loading样式的请求并且请求带错误回调
     */
    protected ComObserver request(OnLoadingListener onLoadingListener, AlertDialog alertDialog, Context context, ProgressManageError progressManageError) {
        return new ComObserver(mCompositeDisposable, onLoadingListener, alertDialog, progressManageError, context, true);
    }


    protected void createBaseData() {
    }
}
